package com.mall4j.cloud.product.controller.admin;



import com.mall4j.cloud.api.product.vo.AttrVO;
import com.mall4j.cloud.api.product.vo.SpuAttrValueVO;
import com.mall4j.cloud.api.product.vo.SpuVO;
import com.mall4j.cloud.common.database.dto.PageDTO;
import com.mall4j.cloud.common.database.vo.PageVO;
import com.mall4j.cloud.common.response.ServerResponseEntity;
import com.mall4j.cloud.common.security.AuthUserContext;;
import com.mall4j.cloud.product.dto.AttrDTO;
import com.mall4j.cloud.product.dto.SpuAttrValueDTO;
import com.mall4j.cloud.product.model.Attr;
import com.mall4j.cloud.product.model.AttrCategory;
import com.mall4j.cloud.product.model.AttrValue;;
import com.mall4j.cloud.product.service.AttrCategoryService;
import com.mall4j.cloud.product.service.AttrService;
import com.mall4j.cloud.product.service.AttrValueService;
import com.mall4j.cloud.product.service.SpuAttrValueService;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 属性
 *
 * @author JCM
 * @date 2021-04-23 9:19:00
 */
@RestController("adminAttrController")
@RequestMapping("/admin/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrCategoryService attrCategoryService;

    @Autowired
    private AttrValueService attrValueService;

    @Autowired
    private SpuAttrValueService spuAttrValueService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @ApiOperation(value = "分页获取属性列表", notes = "分页获取属性列表")
    public ServerResponseEntity<PageVO<AttrVO>> page(@Valid PageDTO pageDTO, AttrDTO attrDTO) {
        attrDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<AttrVO> attrPage = attrService.page(pageDTO, attrDTO);
        return ServerResponseEntity.success(attrPage);
    }

    @GetMapping
    @ApiOperation(value = "获取属性", notes = "根据attrId获取属性")
    public ServerResponseEntity<AttrVO> getByAttrId(@RequestParam Long attrId) {
        return ServerResponseEntity.success(attrService.getByAttrId(attrId));
    }

    @GetMapping("/get_shop_attrs")
    @ApiOperation(value = "获取店铺中的销售属性",notes = "获取店铺中的销售属性")
    public ServerResponseEntity<List<AttrVO>> getShopAttr(AttrDTO attrDTO){
        attrDTO.setShopId(AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success(attrService.list(attrDTO));
    }

    @GetMapping("/get_attrs_by_category_id")
    public ServerResponseEntity<List<AttrVO>> getAttrsByCategoryId(Long categoryId,Integer attrType){
        return ServerResponseEntity.success(attrService.getAttrsByCategoryId(categoryId,attrType));
    }

    @Transactional
    @PostMapping
    @ApiOperation(value = "保存属性", notes = "保存属性")
    public ServerResponseEntity<Void> save(@Valid @RequestBody AttrDTO attrDTO) {
        Attr attr = mapperFacade.map(attrDTO, Attr.class);
        attr.setShopId(AuthUserContext.get().getTenantId());
        attrService.save(attr);

        //创建对应的类并初始化
        //只有基本属性才需与分类关联
        if(attrDTO.getAttrType() == 1) {
            AttrCategory attrCategory = new AttrCategory();
            attrCategory.setAttrId(attr.getAttrId());
            attrCategory.setCategoryId(attrDTO.getCategoryIds().get(0));
            attrCategoryService.save(attrCategory);
        }

        List<AttrValue> attrValues = attrDTO.getAttrValues();
        for(AttrValue value : attrValues){
            value.setAttrId(attr.getAttrId());
            attrValueService.save(value);
        }
        return ServerResponseEntity.success();
    }

    @Transactional
    @PutMapping
    @ApiOperation(value = "更新属性", notes = "更新属性")
    public ServerResponseEntity<Void> update(@Valid @RequestBody AttrDTO attrDTO) {
        Attr attr = mapperFacade.map(attrDTO, Attr.class);
        attrService.update(attr);

        //直接删除原来与属性关联的分类，保存新的
        Long attrId = attrDTO.getAttrId();

        if(attr.getAttrType() == 1) {
            attrCategoryService.deleteByAttrId(attrId);
            List<Long> categoryIds = attrDTO.getCategoryIds();
            for (Long categoryId : categoryIds) {
                AttrCategory attrCategory = new AttrCategory();
                attrCategory.setAttrId(attrId);
                attrCategory.setCategoryId(categoryId);
                attrCategoryService.save(attrCategory);
            }
        }

        attrValueService.deleteByAttrId(attrId);
        List<AttrValue> attrValues = attrDTO.getAttrValues();
        for (AttrValue attrValue : attrValues){
            attrValue.setAttrId(attrId);
            attrValueService.save(attrValue);
        }

        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @ApiOperation(value = "删除属性", notes = "根据属性id删除属性")
    public ServerResponseEntity<Void> delete(@RequestParam Long attrId) {
        SpuAttrValueDTO spuAttrValueDTO = new SpuAttrValueDTO();
        spuAttrValueDTO.setAttrId(attrId);
        List<SpuAttrValueVO> list = spuAttrValueService.list(spuAttrValueDTO);
        if(list.size() != 0){
            return ServerResponseEntity.showFailMsg("该属性正在被使用，无法删除");
        }
        Long shopId = AuthUserContext.get().getTenantId();
        attrService.deleteById(attrId,shopId);
        return ServerResponseEntity.success();
    }

}
